//资质证书返回
$(function(){
	var display=parseQueryString(decodeURI(window.location.href));
	if(display){
		if(display.display=="Y"){
			user();
			showDiv("person-panel");
			
		}
		
	}
})




/*名片拍照*/
$(".change").change(function() {
	var objUrl = getObjectURL(this.files[0]);
	if (objUrl) {
		$('.img0').attr("src", objUrl);
	}
});

/*function uploadImg() {
	if ($("#image").html() != "" || $("#img0").attr("src") != "" || $("#img0").attr("src") != data.imgUrl) {
		$.ajax({
			url : '/pad_marketing/main/updateUser.action',
			data : {
				"image" : $("#img0").attr("src"),
			},
			type : "post",
			success : function(data) {
				closeDiv("person-panel");
			},
			error : function(data) {
				
			}
		})					
	}
	if($("#img0").attr("src") == data.imgUrl || $("#img0").attr("src") == "/pad_marketing/html/img/circle-bird.png")
	{	
		alertMsg("图片格式错误或者为默认图片，请重新上传")
		closeDiv("person-panel");
	}	
}*/

//从资质证书返回时
//将url的参数转为对象
var parseQueryString = function (url) {
    var reg_url = /^[^\?]+\?([\w\W]+)$/,
            reg_para = /([^&=]+)=([\w\W]*?)(&|$)/g, //g is very important
            arr_url = reg_url.exec(url),
            ret = {};
    if (arr_url && arr_url[1]) {
        var str_para = arr_url[1], result;
        while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
        }
    }
    return ret;
};

//点击首页大图像打开个人中心详情
$(document).on("click",".circle-pic",function(){
	user();
})

//个人中心详情
function user(){
	padMarketing.ajaxNormal("/pad_marketing/main/getUser.action", {},
			function(data) {
			console.log(data)
		
				$(".timg").on("click",function(){				
					$("#img0").attr("src",data.imgUrl);
					closeDiv("person-panel");
					$('#openx').hide(200);
				})
				//上传照片
				$("#sub").on("click",function() {
					if ($("#img0").attr("src") != data.imgUrl && $("#img0").attr("src") != '/pad_marketing/html/img/circle-bird.png' && $("#img0").attr("src") != null && $("#img0").attr("src") != "") {
						
						$.ajax({
							url : '/pad_marketing/main/updateUser.action',
							data : {
								"image" : $("#img0").attr("src"),
							},
							type : "post",
							async: true,
							success : function(data) {
								alertMsg("上传成功",0,function(){
									window.location.href="/pad_marketing/index/index.action";
								});							
							},
							error : function(data) {
								alertMsg("图片上传错误");
							}
						})					
					}
					if($("#img0").attr("src") == data.imgUrl || $("#img0").attr("src") == "/pad_marketing/html/img/circle-bird.png")
					{					
						alertMsg("没有上传图片或者为默认图片，请重新上传",0,function(){
							window.location.href="/pad_marketing/index/index.action";
						})
						//window.location.href="/pad_marketing/index/index.action";
					}
				})
				//个人中心详情
				$("#agent").html(data.username.agentName)
				$("#email").html(data.username.email)
				$("#business").html(data.username.businessNo)
				$("#tel").html(data.username.agentTel)
				$("#role").html(data.username.roleName)
				if(data.username.isMDRT == false ){
					$("#mdrtCate").hide();
				}else{
					$("#mdrtCate").show();
				}
			}, function(data) {
				
			}
		)
}
		
function pic() {
	goTakeHeadPhoto("img");
}
function img(src) {
	var img = document.getElementById("img0");
	src = 'data:image/png;base64,' + src;
	img.src = src;
}


function library() {
	openLibrary("imgli");
}

function imgli(src) {
	var img = document.getElementById("img0");
	src = 'data:image/png;base64,' + src;
	img.src = src;
}

// 建立一個可存取到該file的url
function getObjectURL(file) {
	var url = null;
	if (window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if (window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if (window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}

function previewImage(file, prvid) {
	/*
	 * file：file控件 prvid: 图片预览容器
	 */

	var tip = "图片格式不是jpg "; // 设定提示信息
	var filters = {
		"jpeg" : "/9j/4",
		"jpg" : "/9j/4"
	/*
	 * "gif": "R0lGOD", "png": "iVBORw"
	 */
	}
	var prvbox = document.getElementById(prvid)	
	prvbox.innerHTML = '<img src="/pad_marketing/html/img/circle-bird.png" class="img0" id="img0">';
	if (window.FileReader) { // html5方案
		for (var i = 0, f; f = file.files[i]; i++) {
			var fr = new FileReader();
			fr.onload = function(e) {
				var src = e.target.result;
				if (!validateImg(src)) {
					alertMsg(tip);
					$("#img0").attr("src","/pad_marketing/html/img/circle-bird.png");
				} else {
					showPrvImg(src);
				}
			}
			fr.readAsDataURL(f);
		}
	} else { // 降级处理

		if (!/\.jpg$|\.png$|\.gif$/i.test(file.value)) {
			alertMsg(tip);
			$("#img0").attr("src","/pad_marketing/html/img/circle-bird.png");
		} else {
			showPrvImg(file.value);
		}
	}

	function validateImg(data) {
		var pos = data.indexOf(",") + 1;
		for ( var e in filters) {
			if (data.indexOf(filters[e]) === pos) {
				return e;
			}
		}
		return null;
	}

	function showPrvImg(src) {
		/*
		 * var img = document.createElement("img"); img.src = src;
		 * prvbox.appendChild(img);
		 */
		$("#image img").attr("src",src);
	}
}

/*function openAgnMenu() {
	padMarketing.ajaxNormal("/pad_marketing/main/getUser.action", {},
		function(data) {			
			if (data.imgUrl) {
				imgPath = data.imgUrl;
			} else {
				imgPath = "/pad_marketing/html/img/circle-bird.png";
			}
			showAgn(imgPath);
		}
	);
}*/

/*function showAgn(imgPath) {
	var menuHtml = "<div class='circle-panel'>";
	menuHtml += "<nav class='circle-bg'>";
	menuHtml += "<a class='circle-pic' onclick='getUserInfo()'><img src='" + imgPath + "'></a>";
	menuHtml += "<lable class='circle-text'></lable>";
	menuHtml += "<div class='circle-menu'>";
	menuHtml += "<a href='/pad_marketing/about/aboutUs.action' class='menu-item' id='menu-one'><i class=''>公司介绍</i>";
	menuHtml += "<a href='/pad_marketing/main/type005pkgGetHot.action' class='menu-item' id='menu-two'><i class=''>产品中心</i></a>";
	menuHtml += "<a href='javascript:;' onclick='need()' class='menu-item' id='menu-three'><i class=''>需求分析</i></a>";
	menuHtml += "<a href='/pad_marketing/plan/proposalMange.action' class='menu-item' id='menu-four'><i class=''>建议书</i></a>";
	menuHtml += "<a href='/pad_marketing/ordermanager/index.action' class='menu-item' id='menu-five'><i class=''>在线投保</i></a>";
	menuHtml += "</div><span class='close circle-close' onClick=\"closeDiv('agn-menu')\">X</span><a class='out' href='javascript:;' onclick='logOut()'>退出</a></nav></div>";
	document.getElementById("agn-menu").innerHTML = menuHtml;
}*/

/*function getUserInfo() {
	padMarketing.ajaxNormal("/pad_marketing/main/getUser.action", {},
		function(data){
			openUserInfo(data);
		},
		function(data){
			
		}
	);
}*/

//显示用户信息
/*function openUserInfo(data) {
	var imgUrl = "/pad_marketing/html/img/circle-bird.png";
	if(data.imgUrl) {
		imgUrl = data.imgUrl;
	}
	var infoHtml = '<div id="person-panel" class="open-block open-lg hide">';
	infoHtml += '<h4 class="open-header">';
	infoHtml += '<span class="pull-left font-16" onClick="passwordRest()"><span class="icon-edit-gray"></span>&nbsp;修改密码</span>';
	infoHtml += '个人中心<button type="button" class="close" onClick="closeDiv(\'person-panel\')">×</button>';
	infoHtml += '</h4><div class="open-body no-padding">';
	infoHtml += '<div class="padding-2 bg-blue">';
	infoHtml += '<div class="col-md-4 text-right"><div class="blank-10"></div>';
	infoHtml += '<a class="inline-block padding-15 text-center">';
	infoHtml += '<p class="icon icon-circle-pic"></p><p class="font-18 lh-45 white">相册</p>';
	infoHtml += '<input type="file" accept="image/*" class="upfile" onchange="previewImage(this, \'image\')"></a></div>';
	infoHtml += '<div class="col-md-4 text-center">';
	infoHtml += '<div class="pic-panel" id="image"><img src="' + imgUrl + '" class="img0" id="img0"></div></div>';
	infoHtml += '<div class="col-md-4 text-left"><div class="blank-10"></div>';
	infoHtml += '<a class="inline-block padding-15 text-center"><p class="icon icon-circle-camera"></p>';
	infoHtml += '<p class="font-18 lh-45 white">拍照</p><input type="button" class="pic" onclick="pic()"></a></div></div>';
	infoHtml += '<div class="padding-2"><div class="form-group">';
	infoHtml += '<label class="control-label col-md-2 col-md-offset-1" style="margin-left:0%"><span class="pull-left">营销员姓名：</span></label>';
	infoHtml += '<div class="col-md-3" style="width:38%"><p class="form-control-static">' + data.username.agentName + '</p></div>';
	infoHtml += '<label class="control-label col-md-2"><span class="pull-left">公司名称：</span></label>';
	infoHtml += '<div class="col-md-3"><p class="form-control-static">中德安联人寿保险</p></div><div class="clearfix"></div>';
	infoHtml += '<div class="clearfix"></div><label class="control-label col-md-2 col-md-offset-1" style="margin-left:0%"><span class="pull-left">营销员职级：</span></label>';
	infoHtml += '<div class="col-md-3" style="width:38%"><p class="form-control-static">' + data.username.roleName + '</p></div>';
	infoHtml += '<label class="control-label col-md-2"><span class="pull-left">营销员手机：</span></label>';
	infoHtml += '<div class="col-md-3"><p class="form-control-static">' + data.username.agentTel + '</p></div>';
	infoHtml += '<div class="clearfix"></div><label class="control-label col-md-2 col-md-offset-1" style="margin-left:0%"><span class="pull-left">展业证编号：</span></label>';
	infoHtml += '<div class="col-md-3" style="width:38%"><p class="form-control-static">' + data.username.businessNo + '</p></div>';
	infoHtml += '<label class="control-label col-md-2"><span class="pull-left">营销员邮箱：</span></label>';
	infoHtml += '<div class="col-md-3"><p class="form-control-static">' + data.username.email + '</p></div>';
	infoHtml += '<div class="clearfix"></div></div></div></div>';
	infoHtml += '<div class="open-footer"><button class="btn btn-primary" onclick="closeDiv(\'person-panel\')"><span class="icon icon-no"></span>取消</button>';
	infoHtml += '<button onclick="uploadImg()" class="btn btn-warning"><span class="icon icon-ok"></span>确定</button></div></div>';
//	console.log($("#person-panel").html());
	if($("#person-panel").html() == null) {
		$("body").append(infoHtml);
	}
	closeDiv('agn-menu');
	showDiv('person-panel');
}*/

//修改密码
function passwordRest() {
	var passRest = '<div class="open-block open-md hide" id="passwordRest">';
	passRest += '<h4 class="open-header">';
	passRest += '修改密码<button type="button" class="close" onClick="closeDiv(\'passwordRest\')">×</button></h4>';
	passRest += '<div class="open-body"><div class="blank-10"></div>';
	passRest += '<div class="form-group"><label class="col-md-4 control-label">输入旧密码：</label>';
	passRest += '<div class="col-md-7 no-padding"><input class="form-control" id="oldPassword" type="password" required="required" /></div></div>';
	passRest += '<div class="form-group"><label class="col-md-4 control-label">输入新密码：</label>';
	passRest += '<div class="col-md-7 no-padding"><input class="form-control" placeholder="请输入6-20位数字、字母和字符" id="password" oninput="checkStrong(this)"  type="password" required="required" />';
	passRest += '<div class="blank-10"></div> <div class="pw-strength  pw-default pw-medium" id="strongTh">';            
	passRest += '<div class="pw-bar"></div><div class="pw-bar-on"></div>';
	passRest += '<div class="pw-txt"><span>弱</span><span>中</span><span>强</span></div><div class="blank-10"></div></div></div></div>';
	passRest += '<div class="form-group"><label class="col-md-4 control-label">确认新密码：</label>';
	passRest += '<div class="col-md-7 no-padding"><input class="form-control" placeholder="请输入6-20位数字、字母和字符" id="reNewPassword" type="password" required="required" /></div></div>';
	passRest += '<div class="col-md-8 col-md-offset-2"><button type="button" onclick="updatePwd()" class="btn btn-lg btn-warning btn-block">确定</button></div></div></div>';
	if($("#passwordRest").html() == null) {
		$("body").append(passRest);
	} else {
		$("#oldPassword").val("");
		$("#password").val("");
		$("#reNewPassword").val("");
	}
	showDiv('passwordRest');
}

function updatePwd(){
	var chArr = inputCheck("changeForm");
	if(chArr[0])
	{
		alertMsg(chArr[1],1);
		return;
	}
	var oldPassword = $("#oldPassword").val();
	var password    = $("#password").val();
	var reNewPassword = $("#reNewPassword").val();
	if(password!=reNewPassword){
		alertMsg("输入的密码不一致");
		return;
	}
	var user ={};
	user.oldPassword=oldPassword;
	user.password=password;	
	padMarketing.ajaxNormal(
			'/pad_marketing/index/changePassword.action',
			user,
			function (data){	
				alertMsg("恭喜修改成功");
    			closeDiv('passwordReset');
			},
			function(data){
				//console.log(data);
			}
	);
}

//显示更多（修改密码，mdrt,资质证书）
function showBlockIndex(e){
	$("#"+e).toggle();
}
//显示MDRTdiv
function forGetMdrt(){
	padMarketing.ajaxNormal(
			'/pad_marketing/mdrt/getMDRTList.action',{},
			function (data){	
				console.log(data)
				$("#mdrtOverTime").html('最新数据更新至'+data.closeDate);
				var mdrtHtml=""; 
				if(data.mdrtList.length >= 1){
					for(i=0; i<data.mdrtList.length; i++){
						var colorList = ["#f3313d","#66be1f","#00c1cd","#b244a4","#ff5400","#C7A3ED","#CC9898","#8AC007","#CCC007","#FFAD5C"];
						for(var e=0;e<colorList.length;e++){
							var bgColor = getColorByRandom(colorList);
						}
						mdrtHtml+='<div class="divBox">';
						mdrtHtml+='<div class="col-md-4 text-right">';
						mdrtHtml+='<button class="btn btn-circle" style="background:'+bgColor+';"><span class="icon icon-cup"></span></button>';
						mdrtHtml+='</div>';
						mdrtHtml+='<div class="col-md-8">';
						mdrtHtml+='<p class="font-18 b" style="color:'+bgColor+';">'+data.mdrtList[i]+'</p>';
						mdrtHtml+='<p class="font-18 b" style="color:'+bgColor+';">获得MDRT资格</p>';
						mdrtHtml+='</div>';
						mdrtHtml+='<div class="blank-1"></div></div>';
					}
				}else{
					mdrtHtml+='<p class="font-18 b red" style="text-align:center;margin:50px;">暂未获得MDRT资格</p>';
				}
				$("#mdrtBox .divBox").remove();
				$("#mdrtBox").append(mdrtHtml);
			},
			function(data){
				//console.log(data);
			}
	);
	showDiv('mdrt');
}

function getColorByRandom(colorList){  
    var colorIndex = Math.floor(Math.random()*colorList.length);  
    var color = colorList[colorIndex];  
    colorList.splice(colorIndex,1);  
    return color;  
}  







